package com.sskj.gcc.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.common.util.ClickUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.ui.fragment.GameBottomActiveFragment;
import com.sskj.gcc.ui.fragment.GameBottomExchangeFragment;
import com.sskj.gcc.ui.fragment.GameBottomFoodFragment;
import com.sskj.gcc.ui.fragment.GameBottomMallFragment;
import com.sskj.gcc.ui.fragment.GameBottomNotificationFragment;
import com.sskj.gcc.ui.fragment.GameBottomPackageFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GameBottomDialog extends FragmentActivity {

    //公告
    public final static int GAME_NOTIFICATION = 1;
    //动态
    public final static int GAME_ACTIVE = 2;
    //兑换
    public final static int GAME_EXCHANGE = 3;
    //商城
    public final static int GAME_MALL = 4;
    //背包
    public final static int GAME_PACKAGE = 5;
    //领食料
    public final static int GAME_FOOD = 6;


    @BindView(R.id.iv_bottom_logo)
    public
    ImageView ivBottomLogo;
    @BindView(R.id.tv_bottom_title)
    TextView tvBottomTitle;
    @BindView(R.id.iv_bottom_close)
    ImageView ivBottomClose;
    @BindView(R.id.ll_game_bottom_contain)
    LinearLayout llGameBottomContain;
    @BindView(R.id.fl_bottom_base)
    FrameLayout flBottomBase;

    private Fragment currentFragment;

    private GameBottomActiveFragment gameBottomActiveFragment = new GameBottomActiveFragment();
    private GameBottomExchangeFragment gameBottomExchangeFragment = new GameBottomExchangeFragment();
    private GameBottomFoodFragment gameBottomFoodFragment = new GameBottomFoodFragment();
    private GameBottomMallFragment gameBottomMallFragment = new GameBottomMallFragment();
    private GameBottomNotificationFragment gameBottomNotificationFragment = new GameBottomNotificationFragment();
    private GameBottomPackageFragment gameBottomPackageFragment = new GameBottomPackageFragment();

    private View view;

    public GameBottomDialog() {
        super();
    }


    public static void start(Context context, int type) {
        Intent intent = new Intent(context, GameBottomDialog.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = View.inflate(this, R.layout.app_dialog_game_base, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        ClickUtil.click(ivBottomClose, () -> {
            finish();
        });
        if (getIntent().hasExtra("type")) {
            setFragmentType(getIntent().getIntExtra("type", -1));
        }
    }

    public GameBottomDialog setFragmentType(int type) {
        switch (type) {
            case GAME_NOTIFICATION:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_notification));
                tvBottomTitle.setText(R.string.strMainActivityGame0001);
                switchFragment(gameBottomNotificationFragment).commit();
                break;
            case GAME_ACTIVE:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_active));
                tvBottomTitle.setText(R.string.strMainActivityGame0002);
                switchFragment(gameBottomActiveFragment).commit();
                break;
            case GAME_EXCHANGE:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_exchange));
                tvBottomTitle.setText(R.string.strMainActivityGame0003);
                switchFragment(gameBottomExchangeFragment).commit();
                break;
            case GAME_MALL:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_mall));
                tvBottomTitle.setText(R.string.strMainActivityGame0004);
                switchFragment(gameBottomMallFragment).commit();
                break;
            case GAME_PACKAGE:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_package));
                tvBottomTitle.setText(R.string.strMainActivityGame0005);
                switchFragment(gameBottomPackageFragment).commit();
                break;
            case GAME_FOOD:
                ivBottomLogo.setImageDrawable(getResources().getDrawable(R.drawable.app_home_game_food));
                tvBottomTitle.setText(R.string.strMainActivityGame0006);
                switchFragment(gameBottomFoodFragment).commit();
                break;
            default:
                break;
        }
        return this;
    }

    //Fragment优化
    private FragmentTransaction switchFragment(Fragment targetFragment) {
        FragmentTransaction transaction = this.getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            //第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_bottom_base, targetFragment, targetFragment.getClass().getName());
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment);
        }
        currentFragment = targetFragment;
        return transaction;
    }


}
